package com.yic.module.mes.service.processRoute;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.util.collection.CollectionUtils;
import com.yic.module.mes.controller.admin.processrouteinfo.vo.ProcessRouteInfoRespVO;
import com.yic.module.mes.service.itemproduceroute.ItemProduceRouteService;
import com.yic.module.mes.service.processrouteinfo.ProcessRouteInfoService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.mes.controller.admin.processRoute.vo.*;
import com.yic.module.mes.dal.dataobject.processRoute.ProcessRouteDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.mes.convert.processRoute.ProcessRouteConvert;
import com.yic.module.mes.dal.mysql.processRoute.ProcessRouteMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.mes.enums.ErrorCodeConstants.*;

/**
 * 工艺路线 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ProcessRouteServiceImpl implements ProcessRouteService {

    @Resource
    private ProcessRouteMapper processRouteMapper;
    @Resource
    private ProcessRouteInfoService processRouteInfoService;
    @Resource
    @Lazy
    private ItemProduceRouteService itemProduceRouteService;

    @Override
    public Long createProcessRoute(ProcessRouteCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getCode());
        // 插入
        ProcessRouteDO processRoute = ProcessRouteConvert.INSTANCE.convert(createReqVO);
        processRouteMapper.insert(processRoute);
        // 返回
        return processRoute.getId();
    }

    @Override
    public void updateProcessRoute(ProcessRouteUpdateReqVO updateReqVO) {
        // 校验存在
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getCode());
        // 更新
        ProcessRouteDO updateObj = ProcessRouteConvert.INSTANCE.convert(updateReqVO);
        processRouteMapper.updateById(updateObj);
    }

    @Override
    public void deleteProcessRoute(Long id) {
        // 校验存在
        validateProcessRouteExists(id);
        validateProcessRouteUsed(id);
        // 删除
        processRouteMapper.deleteById(id);
    }

    private void validateProcessRouteUsed(Long id) {
        if (itemProduceRouteService.checkUseRoute(id)) {
            throw exception(PROCESS_ROUTE_USED);
        }
    }

    private void validateCreateOrUpdate(Long id, String code) {
        validateCodeExists(id, code);
    }

    private void validateCodeExists(Long id, String code) {
        ProcessRouteDO processRouteDO = processRouteMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(processRouteDO)) {
            return;
        }
        if (id == null) {
            throw exception(PROCESS_ROUTE_CODE_EXISTS);
        }
        if (!id.equals(processRouteDO.getId())) {
            throw exception(PROCESS_ROUTE_CODE_EXISTS);
        }
    }

    private void validateProcessRouteExists(Long id) {
        if (processRouteMapper.selectById(id) == null) {
            throw exception(PROCESS_ROUTE_NOT_EXISTS);
        }
    }

    @Override
    public ProcessRouteDO getProcessRoute(Long id) {
        return processRouteMapper.selectById(id);
    }

    @Override
    public List<ProcessRouteDO> getProcessRouteList(Collection<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return processRouteMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProcessRouteDO> getProcessRoutePage(ProcessRoutePageReqVO pageReqVO) {
        return processRouteMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProcessRouteDO> getProcessRouteList(ProcessRouteExportReqVO exportReqVO) {
        return processRouteMapper.selectList(exportReqVO);
    }

    @Override
    public List<ProcessRouteDetailRespVo> getProcessRouteInfoList(Collection<Long> routeIds) {
        // 查询工艺路线
        List<ProcessRouteDO> processRouteDOS = processRouteMapper.selectBatchIds(routeIds);
        List<ProcessRouteDetailRespVo> respVos = ProcessRouteConvert.INSTANCE.convertList03(processRouteDOS);
        // 查询工艺路线详细配置
        List<ProcessRouteInfoRespVO> processRouteInfoRespVOS = processRouteInfoService.getProcessRouteInfoListByRouteId(routeIds);
        Map<Long, List<ProcessRouteInfoRespVO>> map = CollectionUtils.convertMultiMap(processRouteInfoRespVOS, ProcessRouteInfoRespVO::getRouteId);
        respVos.forEach(processRouteDetailRespVo -> {
            List<ProcessRouteInfoRespVO> list = map.get(processRouteDetailRespVo.getId());
            //TODO 是否排序
            processRouteDetailRespVo.setProcessRouteInfoRespVOS(list);
        });
        return respVos;
    }

}
